
use BetFair::Parser;
use Data::Dumper;

=head1 ABOUT

usage: perl t_parser.pl

This test invokes the BetFair::Parser with a fragment of XML representing the
SOAP response, then retrives various values from it, first by using some standard factory
methods (eg. get_sessionToken, get_responseError), and then by throwing
XPath statements at it, iterating over the results etc.

=cut

# test response xml document
my $xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:n2="http://www.betfair.com/publicapi/types/v2/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><n:getMarketPricesResponse xmlns:n="http://www.betfair.com/publicapi/BFServiceV2/"><n:Result xsi:type="n2:GetMarketPricesResp"><header xsi:type="n2:APIResponseHeader"><errorCode xsi:type="n2:APIErrorEnum">OK</errorCode><minorErrorCode xsi:nil="1"/><sessionToken xsi:type="xsd:string">ubc/ROAWuAnDbCDBWLvqUqivorLNoIsq</sessionToken><timestamp xsi:type="xsd:dateTime">2006-08-01T16:47:35.954Z</timestamp></header><errorCode xsi:type="n2:GetMarketPricesErrorEnum">OK</errorCode><marketPrices xsi:type="n2:MarketPrices"><currencyCode xsi:type="xsd:string">GBP</currencyCode><delay xsi:type="xsd:int">0</delay><discountAllowed xsi:type="xsd:boolean">true</discountAllowed><lastRefresh xsi:type="xsd:long">1154450855786</lastRefresh><marketBaseRate xsi:type="xsd:float">5.0</marketBaseRate><marketId xsi:type="xsd:int">5960762</marketId><marketInfo xsi:type="xsd:string"/><removedRunners xsi:type="xsd:string"/><marketStatus xsi:type="n2:MarketStatusEnum">ACTIVE</marketStatus><numberOfWinners xsi:type="xsd:int">1</numberOfWinners><runnerPrices xsi:type="n2:ArrayOfRunnerPrices"><n2:RunnerPrices xsi:type="n2:RunnerPrices"><asianLineId xsi:type="xsd:int">0</asianLineId><bestPricesToBack xsi:type="n2:ArrayOfPrice"><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">43.42</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">1</depth><price xsi:type="xsd:double">2.66</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">20.99</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">2</depth><price xsi:type="xsd:double">2.62</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">18.32</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">3</depth><price xsi:type="xsd:double">2.6</price></n2:Price></bestPricesToBack><bestPricesToLay xsi:type="n2:ArrayOfPrice"><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">2.07</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">1</depth><price xsi:type="xsd:double">2.68</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">13.88</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">2</depth><price xsi:type="xsd:double">2.7</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">10.93</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">3</depth><price xsi:type="xsd:double">2.72</price></n2:Price></bestPricesToLay><handicap xsi:type="xsd:double">0.0</handicap><lastPriceMatched xsi:type="xsd:double">2.66</lastPriceMatched><reductionFactor xsi:type="xsd:double">0.0</reductionFactor><selectionId xsi:type="xsd:int">148733</selectionId><sortOrder xsi:type="xsd:int">1</sortOrder><totalAmountMatched xsi:type="xsd:double">52.82</totalAmountMatched><vacant xsi:type="xsd:boolean">false</vacant></n2:RunnerPrices><n2:RunnerPrices xsi:type="n2:RunnerPrices"><asianLineId xsi:type="xsd:int">0</asianLineId><bestPricesToBack xsi:type="n2:ArrayOfPrice"><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">14.51</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">1</depth><price xsi:type="xsd:double">2.86</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">6.46</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">2</depth><price xsi:type="xsd:double">2.84</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">26.91</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">3</depth><price xsi:type="xsd:double">2.82</price></n2:Price></bestPricesToBack><bestPricesToLay xsi:type="n2:ArrayOfPrice"><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">55.96</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">1</depth><price xsi:type="xsd:double">3.05</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">2.73</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">2</depth><price xsi:type="xsd:double">6.4</price></n2:Price></bestPricesToLay><handicap xsi:type="xsd:double">0.0</handicap><lastPriceMatched xsi:type="xsd:double">2.86</lastPriceMatched><reductionFactor xsi:type="xsd:double">0.0</reductionFactor><selectionId xsi:type="xsd:int">48044</selectionId><sortOrder xsi:type="xsd:int">2</sortOrder><totalAmountMatched xsi:type="xsd:double">28.84</totalAmountMatched><vacant xsi:type="xsd:boolean">false</vacant></n2:RunnerPrices><n2:RunnerPrices xsi:type="n2:RunnerPrices"><asianLineId xsi:type="xsd:int">0</asianLineId><bestPricesToBack xsi:type="n2:ArrayOfPrice"><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">2.53</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">1</depth><price xsi:type="xsd:double">3.45</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">14.25</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">2</depth><price xsi:type="xsd:double">3.4</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">16.83</amountAvailable><betType xsi:type="n2:BetTypeEnum">L</betType><depth xsi:type="xsd:int">3</depth><price xsi:type="xsd:double">3.35</price></n2:Price></bestPricesToBack><bestPricesToLay xsi:type="n2:ArrayOfPrice"><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">15.0</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">1</depth><price xsi:type="xsd:double">3.5</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">22.49</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">2</depth><price xsi:type="xsd:double">3.55</price></n2:Price><n2:Price xsi:type="n2:Price"><amountAvailable xsi:type="xsd:double">3.0</amountAvailable><betType xsi:type="n2:BetTypeEnum">B</betType><depth xsi:type="xsd:int">3</depth><price xsi:type="xsd:double">3.7</price></n2:Price></bestPricesToLay><handicap xsi:type="xsd:double">0.0</handicap><lastPriceMatched xsi:type="xsd:double">3.45</lastPriceMatched><reductionFactor xsi:type="xsd:double">0.0</reductionFactor><selectionId xsi:type="xsd:int">34</selectionId><sortOrder xsi:type="xsd:int">3</sortOrder><totalAmountMatched xsi:type="xsd:double">17.68</totalAmountMatched><vacant xsi:type="xsd:boolean">false</vacant></n2:RunnerPrices></runnerPrices></marketPrices><minorErrorCode xsi:nil="1"/></n:Result></n:getMarketPricesResponse></soap:Body></soap:Envelope>';

my $p = new BetFair::Parser( { 'message' => $xml } );

my $t = $p->get_sessionToken( );
my $e = $p->get_responseError( $xml );
my $a = $p->get_nodeSet( { 'xpath' => '//n:Result/header/sessionToken' } );
my $b = $p->get_nodeSet( { 'xpath' => '//n:Result/header' } );

print "token => $t \n";
print "error => $e \n";
print "token  => $a \n";
print "header  => $b \n";
foreach my $n ( $b->get_nodelist )
	{
	 print "nodes => \n";
	 print "\t" . XML::XPath::XMLParser::as_string($n) . $/;

	}
#print Dumper $p;
